import React from "react";

import PageContent from '../../PageContent';

import UICardTabs from '../../uicomponents/UICardTabs';

import { ajax, openErrorNotification, openSuccessNotification, showResultMessage, isNotNull, isNull } from '../../common';

import CourseDefineDataTablePanel from './CourseDefineListDataTablePanel';

import CourseTypeDataTablePanel from './CourseTypeListDataTablePanel';

import CourseItemDataTablePanel from './CourseItemListDataTablePanel';

import CoursePriceConfigPanel from './CoursePriceConfigPanel';

/** 
 * 课程定义管理
*/
export default class CourseDefineManageApp extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			currentTabKey: null,
		}
	}

	render() {

		const pageContentMap = {
			'/': {
				text: ' 课程',
				icon: 'book',
				getComponent: function (owner, props) {
					return (
						<CourseDefineTabPanel {...props} pageHistory={props.history} activedTab={owner.currentTabKey} onTabChanged={(key) => owner.currentTabKey = key } />
					);
				}
			},
			'/coursePrice': {
				text: '设置课程价格',
				icon: 'yen-sign',
				getComponent: function (owner, props) {
					return (
						<CoursePriceConfigPanel {...props} />
					);
				}
			}
		}

		return (
			<PageContent {...this.props} pageInstance={this} contentMap={pageContentMap}>

			</PageContent>
		);
	}
}

/**
 * 课程定义标签页
 */
class CourseDefineTabPanel extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			selectedProuctVersion: null,
			activeTabKey : '',
		}
	}

	componentDidMount() {
		
	}

	onTabChange = (activeKey) => {
		
		if ('courseDefineListPanel' === activeKey) {
			this.courseDefineListRef.loadData();
		}
		else if('courseTypeListPanel' === activeKey){
			this.courseTypeListRef.loadData();
		}
		else if('courseItemListPanel' === activeKey){
			this.courseItemListRef.loadData();
		}

		//this.setState({activeTabKey : activeKey});
		if (this.props.onTabChanged) {
			this.props.onTabChanged(activeKey);
		}
	}

	render() {
		const {activeTabKey } = this.state;

		const tabPanelContentMap = {

			'/courseDefineListPanel': {
				getComponent: (owner, props) => {
					return <CourseDefineListPanel {...props} history={this.props.pageHistory} ref={(inst) => this.courseDefineListRef = inst} key="CourseDefineListPanel" />;
				}
			},
			'/courseTypeListPanel': {
				getComponent: (owner, props) => {
					return <CourseTypeListPanel {...props} history={this.props.pageHistory} ref={(inst) => this.courseTypeListRef = inst} key="CourseTypeListPanel" />;
				}
			},
			'/courseItemListPanel': {
				getComponent: (owner, props) => {
					return <CourseItemListPanel {...props} history={this.props.pageHistory} ref={(inst) => this.courseItemListRef = inst} key="CourseItemListPanel" />;
				}
			},

		};

		return (
			<UICardTabs
				bordered={false}
				activeTabKey={this.props.activedTab}
				tabConfigId="ProductUI:courseDefineManageTab"
				title="课程管理"
				onTabChange={this.onTabChange}
				tabContentMap={tabPanelContentMap}
			/>
		);
	}
}

/**
 * 课程定义
 */
class CourseDefineListPanel extends React.Component{

	actionHandler = (actionEvent,panelInstance) => {

		return {
						        
			refresh : () => {
				panelInstance.loadData();
			},
									        
			addNew : () => {
				panelInstance.addNew();
			},
									        
			doDelete : () => {
				panelInstance.doDelete(isNotNull(actionEvent.params) ? actionEvent.params.id : null);
			},
													
			doEdit : () => {
				panelInstance.doEdit(actionEvent.params);
			},						
			doSave : () => {
				panelInstance.doSave();
			},
			setCoursePrice : () => {
				this.props.history.push('/coursePrice',{course : actionEvent.params});
			},
												
		};
	}

	render(){
		return (
			<CourseDefineDataTablePanel actionHandler={this.actionHandler}/>
		);
	}

	loadData = () => {

	}
}

/**
 * 课程类型定义
 */
class CourseTypeListPanel extends React.Component{

	actionHandler = (actionEvent,panelInstance) => {

		return {
						        
			refresh : () => {
				panelInstance.loadData();
			},
									        
			addNew : () => {
				panelInstance.addNew();
			},
									        
			doDelete : () => {
				panelInstance.doDelete(isNotNull(actionEvent.params) ? actionEvent.params.id : null);
			},
													
			doEdit : () => {
				panelInstance.doEdit(actionEvent.params);
			},						
			doSave : () => {
				panelInstance.doSave();
			},
												
		};
	}

	render(){
		return (
			<CourseTypeDataTablePanel actionHandler={this.actionHandler}/>
		);
	}

	loadData = () => {
		
	}
}

/**
 * 课程科目定义
 */
class CourseItemListPanel extends React.Component{

	actionHandler = (actionEvent,panelInstance) => {

		return {
						        
			refresh : () => {
				panelInstance.loadData();
			},
									        
			addNew : () => {
				panelInstance.addNew();
			},
									        
			doDelete : () => {
				panelInstance.doDelete(isNotNull(actionEvent.params) ? actionEvent.params.id : null);
			},
													
			doEdit : () => {
				panelInstance.doEdit(actionEvent.params);
			},						
			doSave : () => {
				panelInstance.doSave();
			},
												
		};
	}

	render(){
		return (
			<CourseItemDataTablePanel actionHandler={this.actionHandler}/>
		);
	}

	loadData = () => {
		
	}
}